package com.rubiksillusions;

public class PositionOfLastLayerEdgesAlgorithm extends Algorithm {
	public String execute(RubiksCube rubiksCube) {
		if (!rubiksCube.testStatus(".y.yyy.y.............bb.......gg.....o..o.......rr....")) {
			for (int i = 0; i < 4; i++) {
				if (
						!rubiksCube.testStatus("....yy.y......................gg................rr....") &&
						!rubiksCube.testStatus("...yy..y......................gg.....o..o.............") &&
						!rubiksCube.testStatus(".y.yy................rr..............o..o.............") &&
						!rubiksCube.testStatus(".y..yy...............bb.........................rr....")
					) {
					rubiksCube.executeMovesList("U ", true);
				}
			}
			
			if (rubiksCube.testStatus("....yy.y......................gg................rr....")) {
				rubiksCube.executeMovesList("Z ", true);
			} else if (rubiksCube.testStatus("...yy..y......................gg.....o..o.............")) {
				rubiksCube.executeMovesList("Z2 ", true);
			} else if (rubiksCube.testStatus(".y.yy................rr..............o..o.............")) {
				rubiksCube.executeMovesList("Z' ", true);
			}
			
			if (!rubiksCube.testStatus(".y.yyy.y.............bb.......gg.....o..o.......rr....")) {
				rubiksCube.executeMovesList("U R' U2 R U R' U R ", true);
				
				if (!rubiksCube.testStatus(".y.yyy.y.............bb.......gg.....o..o.......rr....")) {
					// Ancora non era a posto: quindi ripeto quasi tutto!
					for (int j = 0; j < 4; j++) {
						if (
								!rubiksCube.testStatus("....yy.y......................gg................rr....") &&
								!rubiksCube.testStatus("...yy..y......................gg.....o..o.............") &&
								!rubiksCube.testStatus(".y.yy................rr..............o..o.............") &&
								!rubiksCube.testStatus(".y..yy...............bb.........................rr....")
							) {
							rubiksCube.executeMovesList("U ", true);
						}
					}
					
					if (rubiksCube.testStatus("....yy.y......................gg................rr....")) {
						rubiksCube.executeMovesList("Z ", true);
					} else if (rubiksCube.testStatus("...yy..y......................gg.....o..o.............")) {
						rubiksCube.executeMovesList("Z2 ", true);
					} else if (rubiksCube.testStatus(".y.yy................rr..............o..o.............")) {
						rubiksCube.executeMovesList("Z' ", true);
					}
					
					if (!rubiksCube.testStatus(".y.yyy.y.............bb.......gg.....o..o.......rr....")) {
						rubiksCube.executeMovesList("U R' U2 R U R' U R ", true);
					}
				}
			}
		}
		return rubiksCube.getExecutedMovesList(true);
	}
}
